package com.creating.mict.common;

import java.text.*;
import java.util.*;

/**
 * @ClassName: DateUtils
 * @Description: 多种时间处理方式
 * @author xuelei
 * @date 2012-3-10 下午10:42:38
 * 
 */
public class DateUtils {

	private static SimpleDateFormat datetimeFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss", java.util.Locale.US);
	private static SimpleDateFormat datetimeFormat2 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm", java.util.Locale.US);
	private static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd", java.util.Locale.US);
	private static SimpleDateFormat timeFormat = new SimpleDateFormat(
			"HH:mm:ss", java.util.Locale.US);
	private static SimpleDateFormat dateTimeNumber = new SimpleDateFormat(
			"yyyyMMddHHmmss");
	private static SimpleDateFormat dateTimeNumber2 = new SimpleDateFormat(
			"yyyyMMdd");
	private static SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat(
			"MM-dd HH:mm", java.util.Locale.US);
	private static SimpleDateFormat monthDateFormat = new SimpleDateFormat(
			"MM-dd", java.util.Locale.US);

	private static SimpleDateFormat monthDateFormat2 = new SimpleDateFormat(
			"M月d日", java.util.Locale.US);

	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"HH:mm", java.util.Locale.US);

	/**
	 * 取得当前格式化的日期，时间 HHmm
	 * 
	 * @return
	 */
	public static String formatDateNumber(final java.util.Date date) {
		String str = simpleDateFormat.format(date);
		return str;
	}

	public static String formatDateNumber(final long datetime) {
		String str = simpleDateFormat.format(new Date(datetime));
		return str;
	}

	/**
	 * 取得当前格式化的日期，时间 yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String formatDateTimeNumber(final long datetime) {
		String str = dateTimeNumber.format(new Date(datetime));
		return str;
	}

	/**
	 * 取得当前格式化的日期，时间 yyyyMMdd
	 * 
	 * @return
	 */

	public static String formatDateTimeNumber2(final long datetime) {
		String str = dateTimeNumber2.format(new Date(datetime));
		return str;
	}

	/**
	 * 把日期格式化为yyyy-MM-dd hh:mm
	 * 
	 * @param datetime
	 * @return
	 */
	public static String formatDateTime2(final java.util.Date datetime) {
		String str = datetimeFormat2.format(datetime);
		return str;
	}

	/**
	 * 取得当前格式化的日期，时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrentDateTime() {
		Date now = new Date();
		return formatDateTime(now);
	}

	/**
	 * 取得当前格式化的日期 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getCurrentDate() {
		Date now = new Date();
		return formatDate(now);
	}

	/**
	 * 把格式为yyyy-MM-dd HH:mm:ss转换为Date.
	 * 
	 * @param str
	 * @return
	 */
	public static Date parseDateTime(final String str) {
		Date date = null;
		try {
			date = datetimeFormat.parse(str);
		} catch (ParseException ex) {

		}
		return date;
	}

	/**
	 * 把格式为yyyy-MM-dd转换为Date.
	 * 
	 * @param str
	 * @return
	 */
	public static Date parseDate(final String str) {
		Date date = null;
		try {
			date = dateFormat.parse(str);
		} catch (ParseException e) {

		}
		return date;
	}

	/**
	 * 把日期格式化为 MM月dd日
	 * 
	 * @param datetime
	 * @return
	 */
	public static String formatMonthDate(final long lngTime) {
		String str = monthDateFormat.format(new Date(lngTime));
		return str;
	}

	public static String formatMonthDate2(final long lngTime) {
		String str = monthDateFormat2.format(new Date(lngTime));
		return str;
	}

	public static String formatMonthDate2(final java.util.Date datetime) {
		String str = monthDateFormat2.format(datetime);
		return str;
	}

	/**
	 * 把日期格式化为yyyy-MM-dd hh:mm:ss
	 * 
	 * @param datetime
	 * @return
	 */
	public static String formatDateTime(final java.util.Date datetime) {
		String str = datetimeFormat.format(datetime);
		return str;
	}

	/**
	 * 把日期格式化为MM-dd hh:mm
	 * 
	 * @param datetime
	 * @return
	 */
	public static String formatSimpleDateTime(final java.util.Date datetime) {
		String str = simpleDateTimeFormat.format(datetime);
		return str;
	}

	/**
	 * 把日期格式化为yyyy-MM-dd hh:mm:ss
	 * 
	 * @param datetime
	 *            long
	 * @return
	 */
	public static String formatDateTime(final long datetime) {
		String str = datetimeFormat.format(new Date(datetime));
		return str;
	}

	public static String formatDate(final long date) {
		String str = dateFormat.format(new Date(date));
		return str;
	}

	// 把日期格式化为yyyy-MM-dd格式
	public static String formatDate(final java.util.Date date) {
		String str = dateFormat.format(date);
		return str;
	}

	public static String formatTime(final java.util.Date date) {
		String str = timeFormat.format(date);
		return str;
	}

	public static Date stringToDate(String str) {
		Date date = null;
		try {
			date = dateFormat.parse(str);
		} catch (ParseException e) {

		}
		return date;
	}

	public static Date stringToDateTime(String str) {
		Date date = null;
		try {
			date = datetimeFormat.parse(str);
		} catch (ParseException e) {

		}
		return date;
	}

	/**
	 * @deprecated
	 */
	public static Date StringToDate(String str) {
		Date date = null;
		try {
			date = datetimeFormat.parse(str);
		} catch (ParseException e) {

		}
		return date;
	}

	/**
	 * @deprecated
	 */
	public static String DateToString(java.util.Date date) {
		String str = datetimeFormat.format(date);
		return str;
	}

	/**
	 * 取得当前时间
	 * 
	 * @deprecated
	 */
	public static String getFormattedNowDate() {
		DateFormat df = DateFormat.getDateTimeInstance();
		java.util.Date date = new java.util.Date();
		return df.format(date);
	}

	/**
	 * 取得当前时间之后n分钟时间 日期格式为yyyy-MM-dd hh:mm:ss
	 * 
	 * @deprecated
	 */
	public static String getFormattedNowDateDelayMin(int n) {
		String dateMin = "";
		try {
			// SimpleDateFormat sdf = new
			// SimpleDateFormat("HH:mm:ss.SSS");//时:分:秒.毫秒
			// 当前时间加五分钟
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			GregorianCalendar gc = new GregorianCalendar();
			System.out.println(sdf.format(gc.getTime()));
			// //////////////增加n分钟
			gc.add(GregorianCalendar.MINUTE, n);
			System.out.println(sdf.format(gc.getTime()));
			dateMin = "" + sdf.format(gc.getTime());
			System.out.println(dateMin);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dateMin;
	}

	/**
	 * 取得当前时间之前n分钟时间 日期格式为yyyy-MM-dd hh:mm:ss
	 * 
	 * @deprecated
	 */
	public static String getFormattedNowDateBeforeMin(int n) {
		String dateMin = "";
		try {
			// SimpleDateFormat sdf = new
			// SimpleDateFormat("HH:mm:ss.SSS");//时:分:秒.毫秒
			// 当前时间减五分钟
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 用hh表示用12小时制，HH表示用24小时制。MM定然是大写!

			GregorianCalendar gc = new GregorianCalendar();
			// //////////////减n分钟
			gc.add(GregorianCalendar.MINUTE, -n);

			dateMin = "" + sdf.format(gc.getTime());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dateMin;
	}

	/**
	 * 取得当前时间 yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String formatDateTime() {
		String str = dateTimeNumber.format(new Date());
		return str;
	}

	/**
	 * 判断当前日期是星期几<br>
	 * <br>
	 * 
	 * @return dayForWeek 判断结果<br>
	 * @Exception 发生异常 <br>
	 */
	public static int dayForWeek() throws Exception {
		DateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(fm.parse(getCurrentDate()));// 当前日期
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}

	/**
	 * 推算当前之前或之后多少天的日期,之前用负值
	 * 
	 * @param year
	 *            by number years(before or after this year) default:0
	 * @param month
	 *            by number months(before or after this month)default:0,no
	 *            change
	 * @param day
	 *            by number day(before or after this day)default:0,no change
	 * @return yyyy-MM-dd
	 */
	public static String getCountDate(int year, int month, int day) {

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, year);
		cal.add(Calendar.MONDAY, month);
		cal.add(Calendar.DATE, day);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String temp = format.format(cal.getTime());
		return temp;
	}

	/**
	 * * 取得enddate 之间 startdate的年数 *
	 * 
	 * @param startdate
	 *            Date *
	 * @param enddate
	 *            Date *
	 * @return int
	 */
	public static int getYears(Date startdate, Date enddate) {
		long time = enddate.getTime() - startdate.getTime();
		// int totalS = new Long(time / (1000*60*60*24).intValue(); //天数
		// int totalS = new Long(time / 1000).intValue(); //秒数

		int totalS = new Long(time / (1000 * 60 * 60 * 24 * 365)).intValue();
		return totalS;
	}

	/**
	 * 计算格式为yyyy-MM-dd之前或之后多少天的日期Date.
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateBeforeNum(Date date, int n) {
		// Date date = null;
		String dateStr = "";
		try {
			long mms = 1000 * 60 * 60 * 24 * n;
			// date = dateFormat.parse(str);
			long temp_mms = date.getTime() + mms;

			dateStr = datetimeFormat.format(new Date(temp_mms));
			dateStr = dateStr.substring(0, 10);
		} catch (Exception e) {

		}
		return dateStr;
	}

	/**
	 * @param stype
	 *            返回值类型 0为多少天，1为多少个月，2为多少年
	 * @return
	 */
	public static int compareDate(String date1, String date2, int stype) {
		int n = 0;

		String[] u = { "天", "月", "年" };
		String formatStyle = "yyyy-MM-dd";
		DateFormat df = new SimpleDateFormat(formatStyle);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(date1));
			c2.setTime(df.parse(date2));
		} catch (Exception e3) {
			System.out.println("wrong occured");
		}
		while (!c1.after(c2)) {
			n++;
			if (stype == 1) {
				c1.add(Calendar.MONTH, 1);// 比较月份，月份+1
			} else if (stype == 0) {
				c1.add(Calendar.DATE, 1); // 比较天数，日期+1
			} else if (stype == 2) {
				c1.add(Calendar.YEAR, 1);
			}
		}

		n = n - 1;

		System.out.println(date1 + " -- " + date2 + " 相差多少" + u[stype] + ":"
				+ n);
		return n;
	}

	/**
	 * 
	* @Title: getTransactionIdByTime 
	* @Description: 更具当前时间和随机数产生唯一id
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String getTransactionIdByTime() {
		Calendar day = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String times_str = sdf.format(day.getTime());
		int temp_num = (int) (Math.random() * 10000);
		return times_str + temp_num;
	};
	
	/**
	 * 
	* @Title: getTransactionIdByTime 
	* @Description: 更具当前时间和随机数产生唯一id
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public static String getTransactionIdByLongTime() {
		Calendar day = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String times_str = sdf.format(day.getTime());
		int temp_num = (int) (Math.random() * 10000000);
		return times_str + temp_num;
	};

	public static void main(String[] args) {

		String temp = getDateBeforeNum(new Date(), 6);

		System.out.println("=============------" + temp);

		// SimpleDateFormat sdf = new
		// SimpleDateFormat("HH:mm:ss.SSS");//时:分:秒.毫秒
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		// GregorianCalendar gc = new GregorianCalendar();
		// System.out.println(sdf.format(gc.getTime()));
		// ////////////////增加2分钟
		// gc.add(GregorianCalendar.MINUTE, -2);
		// System.out.println(sdf.format(gc.getTime()));

	}
}
